package com.raven.craft.code.generator.project.antijson.type;

import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Getter
@Setter
@ToString
public class ArrayType implements Type, Mergeable {
    protected Type elementType;
    private Class<?> collectionType = List.class;

    public ArrayType(Type elementType) {
        this.elementType = elementType;
    }

    @Override
    public Set<String> getImports() {
        if (elementType == null) return Collections.emptySet();
        HashSet<String> set = Sets.newHashSet(collectionType.getName());
        set.addAll(elementType.getImports());
        set.add(collectionType.getName());
        return set;
    }

    @Override
    public String getName() {
        return String.format("%s<%s>", collectionType.getSimpleName(), elementType == null ? "?" : elementType.getName());
    }

    public void merge(Type other) {
        this.elementType = Type.reduce(elementType, other);
    }
}